﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ERPMVC.Models;

namespace ERPMVC.Controllers
{
    public class ContasaReceberController : Controller
    {
        //
        // GET: /ContasaReceber/
        private ContasDBEntities _db = new ContasDBEntities();
        public ActionResult Index()
        {
            return View(_db.contasareceber.ToList());
        }

        //
        // GET: /ContasaReceber/Details/5

        public ActionResult Details(int id)
        {
            var conta = _db.contasareceber.First(x => x.ContaID == id);
            if (conta == null)
                return View("NotFound");

            return View(conta);
        }

        //
        // GET: /ContasaReceber/Create

        public ActionResult Create()
        {
            ViewData["DocumentoDropDown"] = _db.DocumentoDropDown.Select(m => new SelectListItem
                              {
                                  Text = m.DocumentoDropDown1,
                                  Value = m.DocumentoDropDown1
                              })
                               .ToList();
            
            return View();
        } 

        //
        // POST: /ContasaReceber/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Exclude = "Id")] contasareceber contaCriada)
        {

            if (!ModelState.IsValid)
                return View();

            _db.AddTocontasareceber(contaCriada);
            _db.SaveChanges();

            return RedirectToAction("Index");
        }

        //
        // GET: /ContasaReceber/Delete/5
 
        public ActionResult Delete(int id)
        {
            var conta = _db.contasareceber.First(x => x.ContaID == id);
            if (conta == null)
                return View("NotFound");

            return View(conta);
        }

        //
        // POST: /ContasaReceber/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            var conta = _db.contasareceber.First(x => x.ContaID == id);
            if (conta == null)
                return View("NotFound");
            _db.DeleteObject(conta);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        //
        // GET: /ContasaReceber/Edit/5
 
        public ActionResult Edit(int id)
        {
            var contaEditar = (from m in _db.contasareceber
                               where m.ContaID == id
                               select m).First();
            return View(contaEditar);
        }

        //
        // POST: /ContasaReceber/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(contasareceber contaToEdit)
        {

            var originalConta = (from m in _db.contasareceber
                                 where m.ContaID == contaToEdit.ContaID
                                 select m).First();

            if (!ModelState.IsValid)
                return View(originalConta);
            _db.ApplyPropertyChanges(originalConta.EntityKey.EntitySetName, contaToEdit);
            _db.SaveChanges();
            return RedirectToAction("Index");

        }
    }
}
